﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using attendance.L2Busslogic;
using attendance.L3Bussobject;

namespace attendance.L4View.master
{
    public partial class master_import_info : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (student_infoBL.deleteAll())
            {
                if (!FileUpload1.HasFile)
                {
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('请选浏览要上传的文件');</script>");
                    return;
                }
                string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);
                if (fileExtension != ".xls")
                {
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('文件上传类型不正确，请上传excel文件格式');</script>");
                    return;
                }
                //string ssname1= System.IO.Path.GetFileName(FileUpload1.FileName);
                // string ssname = System.IO.Path.GetFullPath(FileUpload1.FileName);
                string ss1 = "exceteachertest.xls";
                FileUpload1.PostedFile.SaveAs(Server.MapPath(ss1));

                string strCon;
                // strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/Layer4View/InputData/Uploadfile/excelyccztctloadtest.xls") + ";Extended Properties=Excel 8.0;";
                // strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ssname + ";Extended Properties=Excel 8.0;";
                strCon = "Provider=Microsoft.ACE.OleDb.12.0;Data Source=" + Server.MapPath("~/student_info.xls") + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";

                OleDbConnection con = new OleDbConnection(strCon);
                OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                int dd = ds.Tables[0].Rows.Count;
                student_infoOB myword = new student_infoOB();
                if (dd > 0)
                {
                    //     if (SWUBL.DeleteSwuTeacher2())
                    //   {
                    for (int i = 0; i < dd; i++)
                    {
                        if (ds.Tables[0].Rows[i][0].ToString().Trim().Length == 0 || ds.Tables[0].Rows[i][1].ToString().Trim().Length == 0)
                        {
                            continue;
                        }
                        myword.name = ds.Tables[0].Rows[i][0].ToString().Trim();
                        myword.numb = ds.Tables[0].Rows[i][1].ToString().Trim();
                        student_infoBL.Addfill(myword);
                    }
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('导入成功');{location.href='SWUEditTeacher.aspx'}</script>");

                    //  }
                    //  else
                    //  {
                    //      Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('导入失败！');</script>");

                    //  }

                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('导入失败！');</script>");
                }
            }
        }
    }
}